import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { IView } from "../../mvc/interfaces/IView";
import { ChristmasTreeModule } from "../../../modules/ChristmasTreeModule";
import { DataEvent } from "../events/DataEvent";
import { ELottertType } from "../../../Message/Public/ELottertType";
import { SPublicNotice } from "../../../Message/Public/SPublicNotice";
import { ExplorerConfig } from "../resource/ExplorerConfig";
import { EShowArea } from "../../../Message/Public/EShowArea";
import { ItemData } from "../resource/info/ItemData";
import { SMoney } from "../../../Message/Game/SMoney";
import { SShakeTreeNum } from "../../../Message/Public/SShakeTreeNum";
import { IChristmasTreeModule } from "../../../modules/interfaces/IChristmasTreeModule";
type int = number;
//class ChristmasController
    
    export  class ChristmasController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.ChristmasTreeOpen, this.onChristmasTreeOpenHandler);
            Dispatcher.addEventListener(EventName.TreasureGetRecordsSuccess, this.onGetRecordsSuccessHandler);
            Dispatcher.addEventListener(EventName.TreasureExplorerRumorAdd, this.onRecordsAddHandler);
            Dispatcher.addEventListener(EventName.ChangeScene, this.onChangeSceneHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackChristmasCommand, this.onBackPackChristmasCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.LotteryActiveRewardFlyToBag, this.onFlyToBagHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackTreasurePackChange, this.onTreasurePackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.ShakeTreeNumUpdate, this.onShakeTreeNumUpdate);
            Dispatcher.addEventListener(EventName.CurWorldLevelUpdate, this.onCurWorldLevelUpdateHandler);
            return;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            return;
        }

        protected /* override */ initView(): IView
        {
            if (this._christmasTreeModule == null) 
            {
                this._christmasTreeModule = new ChristmasTreeModule();
                _view = this._christmasTreeModule;
            }
            return this._christmasTreeModule;
        }

        /* internal  */onChristmasTreeOpenHandler(arg1: DataEvent): void
        {
            this.initView().show();
            this._christmasTreeModule.updateMoney(cache.role.money.gold);
            this._christmasTreeModule.updateCapacity();
            if (cache.shakeTree.shakeInfo) 
            {
                this._christmasTreeModule.updateNumTxt(cache.shakeTree.shakeInfo.num, cache.shakeTree.shakeInfo.perNum);
            }
            return;
        }

        /* internal  */onGetRecordsSuccessHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=arg1.data["type"] as int;
            if (loc1 == ELottertType._ELotterySpringFestival) 
            {
                loc2 = arg1.data["records"] as Array<any>;
                if (this._christmasTreeModule && this._christmasTreeModule.isHide == false) 
                {
                    this._christmasTreeModule.updateRecords(loc2);
                }
            }
            return;
        }

        /* internal  */onRecordsAddHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPublicNotice;
            var loc2=ELottertType._ELotterySpringFestival;
            var loc3;
            if (loc3 = ExplorerConfig.instance.getExplorerInfoByActiveWorldLevel(cache.role.activeWorldLevel)) 
            {
                loc2 = loc3.type;
            }
            if (loc1.area & EShowArea._EShowAreaExplorer && loc1.code == loc2) 
            {
                if (this._christmasTreeModule && this._christmasTreeModule.isHide == false) 
                {
                    this._christmasTreeModule.addRecord(loc1);
                }
            }
            return;
        }

        /* internal  */onBackPackChristmasCommandHandler(arg1: Object=null): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            if (this._christmasTreeModule && this._christmasTreeModule.isHide == false) 
            {
                var loc4=0;
                var loc5=arg1;
                for(loc2 of loc5) 
                {
                    if (!((loc3 = loc2.updateAmount) > 0)) 
                    {
                        continue;
                    }
                    loc1 = {"updateAmount": loc3, "itemData": loc2.itemData, "code": loc2.itemData.itemCode};
                    this._christmasTreeModule.addGetItem(loc1);
                }
            }
            return;
        }

        /* internal  */onChangeSceneHandler(arg1: DataEvent): void
        {
            if (this._christmasTreeModule && this._christmasTreeModule.isHide == false) 
            {
                this._christmasTreeModule.hide();
            }
            return;
        }

        /* internal  */onFlyToBagHandler(arg1: ItemData): void
        {
            if (this._christmasTreeModule && !this._christmasTreeModule.isHide && arg1) 
            {
                this._christmasTreeModule.flyItem(arg1.itemCode);
            }
            return;
        }

        /* internal  */onMoneyUpdateHandler(arg1: Object): void
        {
            var loc1=arg1 as SMoney;
            if (this._christmasTreeModule) 
            {
                this._christmasTreeModule.updateMoney(loc1.gold);
            }
            return;
        }

        /* internal  */onShakeTreeNumUpdate(arg1: SShakeTreeNum): void
        {
            if (this._christmasTreeModule) 
            {
                this._christmasTreeModule.updateNumTxt(arg1.num, arg1.perNum);
            }
            return;
        }

        /* internal  */onTreasurePackChangeHandler(arg1: Object): void
        {
            if (this._christmasTreeModule) 
            {
                this._christmasTreeModule.updateCapacity();
            }
            return;
        }

        /* internal  */onCurWorldLevelUpdateHandler(arg1: DataEvent): void
        {
            if (isViewShow) 
            {
                this._christmasTreeModule.refreshData();
            }
            return;
        }

        private /* var */_christmasTreeModule: IChristmasTreeModule;
    }
